<?php
require_once("../Info.php");
require_once("Cache.php");

class Artist {
	
	public $name;
    public $weight;
	public $matchingArtists; //used for the presentation of the linup
    public $url;
	
    public $cachePathSimiliarArtist = "../cache/artist/similar/";
    
    private $similarArtists;
    private $bioSummary;
    private $bioFulltext;
    private $bioTimestap;
    private $avatarThumbnail;
    private $avatarFullSize;
    public $note;
	
	public function Artist($name = "") {
		$this->name = $name;
	}
	
	public function getSimilarArtits($limit = 100) {
	    if(!empty($this->similarArtists)) {
	       return $this->similarArtists;
	    }
		if(empty($this->name)) {
			throw new Exception("object artist name not set!");
		}
		$file = $this->cachePathSimiliarArtist.urlencode($this->name).".serialized";
        if (@fopen($file, "r")) {
            $this->similarArtists = unserialize(file_get_contents($file));
            return $this->similarArtists;
        } else {
            $c = Cache::getInstance();
            $c->queueSimilarArtists(urlencode($this->name));
            $c->startCaching();
            /*
	        $xmlUrl = "http://ws.audioscrobbler.com/2.0/?method=artist.getsimilar&artist=".$row['artistname']."&api_key=519681edb551a3b5177bb67352e16136&limit=150";
	        $xml = file_get_contents($xmlUrl);
	        $fp = @fopen($file, "w");
	        if($fp) {
	            $result = fwrite($fp, $xml);
	            fclose($fp);
	        }
            */
        }
	}
	
	function getInfo() 
	{
        if(empty($this->name)) {
            throw new Exception("object artist name not set!");
        }
	    $file = "./cache/artist/info/".urlencode(strtolower($this->name)).".xml";
	    //Info::dump($file);
        if (@fopen($file, "r")) {
            $sxml = simplexml_load_file($file);
        } else {
        	/*
            $c = Cache::getInstance();
            $c->queueArtistInfo(urlencode($this->name));
            $c->startCaching();
            */
            $xmlUrl = "http://ws.audioscrobbler.com/2.0/?method=artist.getinfo&artist=".urlencode(strtolower($this->name))."&api_key=519681edb551a3b5177bb67352e16136";
            $xml = file_get_contents($xmlUrl);
            $fp = @fopen($file, "w");
		    if($fp) {
		        $result = fwrite($fp, $xml);
		        fclose($fp);
            }
            $sxml = new SimpleXMLElement($xml);
            
        }
        if(empty($sxml)) {
            return;
        }
        foreach($sxml->artist as $obj) {
            $this->url = (string)$obj->url;
            $this->avatarFullSize = (string)$obj->image[2];
            $this->saveAvatarThumb($this->avatarFullSize);
            $this->bioTimestap = (string)$obj->bio->published;
            $this->bioSummary = (string)$obj->bio->summary;
            $this->bioFulltext = (string)$obj->bio->content;
        }
	}
	
	function getBioSummary()
	{
	   if(empty($this->bioSummary)) {
           $this->getInfo();
       }
       return $this->bioSummary;
	}

   function getBioFulltext()
    {
       if(empty($this->bioFulltext)) {
           $this->getInfo();
       }
       return $this->bioFulltext;
    }

   function getAvatarThumbnail()
    {
       if(empty($this->avatarThumbnail)) {
           $this->getInfo();
       }
       return $this->avatarThumbnail;
    }
    
   function getAvatarFullSize()
    {
       if(empty($this->avatarFullSize)) {
           $this->getInfo();
       }
       return $this->avatarFullSize;
    }
    
    function saveAvatarThumb($imgUrl) {
        $imageFile = "./cache/artist/avatar/".md5(strtolower($this->name))."_thumbnail.jpg";
        if (!@fopen($imageFile, "r")) {
            //Info::dump("Transforming avatar for artist ".$artist.": ".$image);
            $image_transform =& Image_Transform::factory('');
            $image_transform->load($imgUrl);
            $image_transform->resize(50,50);
            //$image_transform->scaleMaxLength('50');
            $image_transform->save($imageFile,'jpg');
            if($image_transform->isError()){
                trigger_error($image_transform->getMessage(), E_USER_ERROR);
            }
        }
        $this->avatarThumbnail = $imageFile;
    }
}
?>